﻿using System;
using System.Collections.Generic;
using System.Linq;
using NetSurveillance.WebApp.DataServices.Models;
using ServiceStack.OrmLite;

namespace NetSurveillance.WebApp.DataServices.Implementations
{
    public class HostService : BaseService<Host>
    {
        public HostService(string connStr, IOrmLiteDialectProvider provider = null)
            : base(connStr, provider)
        {

        }

        public List<Host> GetHostsByTaskId(long taskId)
        {
            using (var db = DbFactory.OpenDbConnection())
            {
                var hosts = db.Select<Host>(p => p.StatusToken != 0 && p.TaskId == taskId);
               
                return hosts;
            }
        }

        public IList<Host> GetHostsByMacs(string[] macArr)
        {
            using (var db = DbFactory.OpenDbConnection())
            {
                string searchSql =
                    string.Format(
                        @"select * from ""Hosts"" where ""StatusToken""!=0 and ""Mac"" in ({0})",
                        string.Format("'{0}'", string.Join("','", macArr)));
                var hosts = db.SqlList<Host>(searchSql);
                return hosts;
            }
        } 

        public ServiceResult SaveVirusEmbedHost(long taskId, long[] hostIds)
        {
            using (var db = DbFactory.OpenDbConnection())
            {
                using (var trans = db.OpenTransaction())
                {
                    try
                    {
                        var veHosts = db.Select<VirusEmbedHost>(p => p.TaskId == taskId);
                        //数据库中已有
                        foreach (var veHost in veHosts)
                        {
                            veHost.StatusToken = hostIds.Contains(veHost.HostId) ? 1 : 0;
                            veHost.LastModifiedTime = DateTime.Now;
                            db.Save(veHost);
                        }
                        //数据库中未有
                        bool isDbEmptry = !veHosts.Any();
                        foreach (long hostId in hostIds.Where(p => isDbEmptry || veHosts.All(ve => ve.HostId != p)))
                        {
                            var veHost = new VirusEmbedHost {HostId = hostId, TaskId = taskId};
                            db.Save(veHost);
                        }
                    }
                    catch (Exception ex)
                    {
                        //todo:记录ex问题
                        trans.Rollback();
                        return new ServiceResult {ExecutedSuccess = false, Message = ex.Message};
                    }
                    trans.Commit();
                }
                return ServiceResult.SuccessResult;
            }
        }

        public IEnumerable<long> GetVirusEmbedHostIds(long taskId)
        {
            using (var db = DbFactory.OpenDbConnection()) {
                var hostIds =
                    db.Select<VirusEmbedHost>(p => p.StatusToken != 0 && p.TaskId == taskId).Select(p => p.HostId);
                return hostIds;
            }
        }

        public ServiceResult SaveQQMonitorHost(long taskId, long[] hostIds)
        {
            using (var db = DbFactory.OpenDbConnection())
            {
                using (var trans = db.OpenTransaction())
                {
                    try
                    {
                        var qqHosts = db.Select<QQMonitorHost>(p => p.TaskId == taskId);
                        //数据库中已有
                        foreach (var qqHost in qqHosts)
                        {
                            qqHost.StatusToken = hostIds.Contains(qqHost.HostId) ? 1 : 0;
                            qqHost.LastModifiedTime = DateTime.Now;
                            db.Save(qqHost);
                        }
                        //数据库中未有
                        bool isDbEmptry = !qqHosts.Any();
                        foreach (long hostId in hostIds.Where(p => isDbEmptry || qqHosts.All(ve => ve.HostId != p)))
                        {
                            var qqHost = new QQMonitorHost { HostId = hostId, TaskId = taskId };
                            db.Save(qqHost);
                        }
                    }
                    catch (Exception ex)
                    {
                        //todo:记录ex问题
                        trans.Rollback();
                        return new ServiceResult {ExecutedSuccess = false, Message = ex.Message};
                    }
                    trans.Commit();
                }
                return ServiceResult.SuccessResult;
            }
        }
 
        public IEnumerable<long> GetQQMonitorHostIds(long taskId)
        {
            using (var db = DbFactory.OpenDbConnection())
            {
                var hostIds =
                    db.Select<QQMonitorHost>(p => p.StatusToken != 0 && p.TaskId == taskId)
                      .Select(p => p.HostId);
                return hostIds;
            }
        }
    }
}
